package Test;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import Container.Bid;
import Container.Center;
import Container.Criteria;
import Container.Item;
import Container.Login;
import Container.Message;
import Container.Transaction;
import Container.User;
import Container.UserInfo;
import DBControl.SimpleJDBC;
import DBControl.DBInterface;

public class TestSet {
	DBInterface s = null;
	
	public static void main(String[] args) throws SQLException {
		TestSet ts = new TestSet();
		ts.testCase6();
		//ts.testCase5();
	}

	public TestSet() {
		s = new SimpleJDBC();
	}
	
	//User Register
	private void testCase1() throws SQLException {
		User user = new User("test5", "test5");
		UserInfo info = new UserInfo("dummy5", "5193214321", "toronto", "dummy5@uw.ca");
		
		int uid = s.register(user, info);
		System.out.println("Uid: " + uid);
	}
	
	//User Login
	private void testCase2() throws SQLException {
		User user = new User("test5", "test4");
		
		Login login = s.login(user);
		
		if (login == null) {
			System.out.println("Error");
		} 
		else {
			if (login.getUid() == -1) {
				System.out.println("Wrong username or password");
			}
			else {
				System.out.println("Success!\nuid: " + login.getUid());
				System.out.println("Name: " + login.getName());
			}
		}
	}
	
	//Add Category
	private void testCase3() throws SQLException {
		s.addCategory("Axe");
		s.addCategory("Sword");
		s.addCategory("Mace");
	}
	
	//Retrieve Category
	private void testCase4() throws SQLException {
		HashMap<String, Integer> result = s.getCategory();
		
		Iterator<String> it = result.keySet().iterator();
		
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}
	
	//Add Item
	private void testCase5() throws SQLException {
		Item item = new Item("Happy Mace", null, "Light weapon", 1, 200, 300, 3 ,3);
		s.addItem(item);
	}
	
	//Add comment
	private void testCase6() throws SQLException {
		s.comment(1, 1, "Nice!");
		//s.comment(4, 8, "Great!");
	}
	
	//Get item detail
	private void testCase7() throws SQLException {
		Item item = s.viewDetail(1);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
		
		System.out.println("iid: " + item.getIid());
		System.out.println("name: " + item.getName());
		System.out.println("category: " + item.getCategoryname());
		System.out.println("description: " + item.getDescription());
		System.out.println("seller id: " + item.getSeller());
		System.out.println("bidder id: " + item.getBidder());
		System.out.println("co: " + item.getCo());
		System.out.println("bo: " + item.getBo());
		System.out.println("Start Date: " + format.format(item.getStart()));
		System.out.println("End Date: " + format.format(item.getEnd()));
		System.out.println("\nComments: ");
		
		ArrayList<Message> comment = item.getComment();
		if (comment != null) {
			for (int i = 0; i < comment.size(); i++) {
				System.out.println("User name: " + comment.get(i).getUsername());
				System.out.println("Date: " + format.format(comment.get(i).getDate()));
				System.out.println("Comment: " + comment.get(i).getComment());
				System.out.println();
			}
		}
	}
	
	//Search
	private void testCase8() throws SQLException {
		Criteria cri = new Criteria();
		cri.setName("more");
		//cri.setCategory(1);
		//cri.setMin(50);
		//cri.setMax(90);
		
		ArrayList<Item> result = s.search(cri);
		Item item = null;
		
		if (result != null) {
			for (int i = 0; i < result.size(); i++) {
				item = result.get(i);
				System.out.println("name: " + item.getName());
				System.out.println("co: " + item.getCo());
				System.out.println("bo: " + item.getBo());
				System.out.println();
			}
		}
		else {
			System.out.println("No result!");
		}
	}
	
	//Bid
	private void testCase9() throws SQLException {
		System.out.println("Result: " + s.bidItem(3, 11, 250));
	}
	
	//Buyout
	private void testCase10() throws SQLException {
		System.out.println("Result: " + s.buyout(1, 10));
	}
	
	//Check Completion
	private void testCase11() throws SQLException {
		System.out.println("Result: " + s.checkCompletion());
	}
	
	//View Center
	private void testCase12() throws SQLException {
		Center center = s.viewCenter(3);
		
		UserInfo info = center.getUserinfo();
		System.out.println("User Info:\nName: " + info.getName());
		System.out.println("Telephone: " + info.getTel());
		System.out.println("Address: " + info.getAddress());
		System.out.println("E-mail: " + info.getEmail());
		
		System.out.println("Selling Items");
		ArrayList<Item> items = center.getItems();
		for (int i = 0; i < items.size(); i++) {
			Item item = items.get(i);
			System.out.print("iid: " + item.getIid());
			System.out.println("    Item Name: " + item.getName());
			System.out.println();
		}
		
		System.out.println("Bidding Items");
		ArrayList<Bid> bids = center.getBids();
		for (int i = 0; i < bids.size(); i++) {
			Bid bid = bids.get(i);
			System.out.print("Item Name: " + bid.getItemname());
			System.out.println("    Amount: " + bid.getAmount());
			System.out.println("    Date: " + bid.getDate().toString());
			System.out.println();
		}
		
		System.out.println("Won Items");
		ArrayList<Transaction> buys = center.getTrans_Buy();
		for (int i = 0; i < buys.size(); i++) {
			Transaction transbuy = buys.get(i);
			System.out.print("Item Name: " + transbuy.getItemname());
			System.out.println("    Seller Name: " + transbuy.getSellername());
			System.out.println("    Price: " + transbuy.getAmount());
			System.out.println("    Date: " + transbuy.getDate().toString());
			System.out.println();
		}
		
		System.out.println("Sold Items");
		ArrayList<Transaction> sells = center.getTrans_Sell();
		for (int i = 0; i < sells.size(); i++) {
			Transaction transsell = sells.get(i);
			System.out.print("Item Name: " + transsell.getItemname());
			System.out.println("    Price: " + transsell.getAmount());
			System.out.println("    Date: " + transsell.getDate().toString());
			System.out.println();
		}
	}
}
